var TestMgtController = (function () {

	var currentTest;

	//Start - Bind handlers

	$("#accountLink").live('click', function () {
		$(".content").load("../test/subscribed_tests.html");
	});

	$(".category-link").live('click', function () {
		$(".content-holder").load("../test/test_listings.html");
	});

	$(".test-details-link").live('click', function () {
		var testCode = $(this).data("code");
		$(".all_tests").empty();
		$(".all_tests").load("../test/test_details.html #testDetails", function () {
			$("#testDetails #subscribeBtn").data("code", testCode);
		});
	});

	$("#testDetails #subscribeBtn").live('click', function () {
		alert("Trying to subscribe");
	});

	$(".start-test").live('click', function () {
		var testWindow = window.open("../test/test_screen.html", "", "height:800, width:800");
		if (window.focus) {
			testWindow.focus();
		}
	});

	//End - Bind handlers

	getCurrentTest = function () {
		return currentTest;
	}

	getTestCategories = function () {
		TestMgtService.getTestCategories(getTestCategories_Result);
	};

	getTestCategories_Result = function (data) {
		//Register the partial
		Handlebars.registerPartial("categories-recursion", $("#categories-recursion-partial").html());
		var src = $("#categories-list-tmpl").html();
		var template = Handlebars.compile(src);
		alert(template(data));
		$("#categoryPane").append(template(data));

		//Bind the selected event on the tree and open the relevant node details in the main section
		$("#categoryPane").bind("select_node.jstree", function (event, data) {
			var name = data.rslt.obj.attr("data-name");
			var type = data.rslt.obj.attr("data-type");
			//alert(name + "  " + type);
			getTestsInCategory(name);
		});

		$("#categoryPane").jstree({
			"themes": {
				"theme": "default",
				"url": "../../lib/jstree/themes/default/style.css",
				"dots": false,
				"icons": true
			},

			"ui": {
				"select_limit": 1,
				"selected_parent_close": "select_parent"
			},
			"plugins": ["themes", "html_data", "ui"]
		});
	};

	getAllTests = function () {
		TestMgtService.getAllTests(getAllTests_Result);
	};

	getAllTests_Result = function (data) {
		var src = $("#tests-overview-list-tmpl").html();
		var template = Handlebars.compile(src);
		$(".all_tests").empty();
		$(".all_tests").append(template(data));

		$('.test-rating').raty({
			score: function () {
				return $(this).attr('data-rating');
			},
			numberMax: 5,
			starOff: "../../assets/img/star-off.png",
			starOn: "../../assets/img/star-on.png",
			starHalf: "../../assets/img/star-half.png",
			halfShow: true,
			width: 150,
		});

		//testRepoMasonry();

		$('.description').dotdotdot();
	};

	getTestsInCategory = function (categoryName) {
		TestMgtService.getTestsInCategory(categoryName, getTestsInCategory_Result);
	};

	getTestsInCategory_Result = function (data) {
		//alert(JSON.stringify(data));
		var src = $("#tests-overview-list-tmpl").html();
		var template = Handlebars.compile(src);
		$(".all_tests").empty();
		$(".all_tests").append(template(data));
	};

	viewTestDetails = function () {

	};

	subscribeToTest = function (testCode) {

	};

	getSubscribedTests = function () {
		TestMgtService.getSubscribedTests(getSubscribedTests_Result);
	}

	getSubscribedTests_Result = function (data) {
		var src = $("#subscribed-tests-tmpl").html();
		var template = Handlebars.compile(src);
		$("#subscribedTests").empty();
		$("#subscribedTests").append(template(data));
	};

	getTestDetails = function () {
		TestMgtService.getTestDetails("dummy", getTestDetails_Result);
	};

	getTestDetails_Result = function (data) {
		var src = $("#test-instructions-tmpl").html();
		var template = Handlebars.compile(src);
		$("#test").append(template(data));
		currentTest = data;
	}

	testRepoMasonry = function () {
		$('.tests').masonry({
			// options
			itemSelector: '.test',
			columnWidth: function (containerWidth) {
				return containerWidth / 3;
			}
		});
	}

	return {
		getTestCategories: getTestCategories,
		getTestCategories_Result: getTestCategories_Result,
		getAllTests: getAllTests,
		getAllTests_Result: getAllTests_Result,
		getTestsInCategory: getTestsInCategory,
		getTestsInCategory_Result: getTestsInCategory_Result,
		getSubscribedTests: getSubscribedTests,
		getSubscribedTests_Result: getSubscribedTests_Result,
		getTestDetails: getTestDetails,
		getTestDetails_Result: getTestDetails_Result,
		getCurrentTest: getCurrentTest
	};


})();